package com.qfedu.edu.orderservice.slot.impl;

import com.qfedu.edu.constance.RedisConstance;
import com.qfedu.edu.enum1.BuyStatus;
import com.qfedu.edu.manager.RedisManager;
import com.qfedu.edu.orderservice.slot.generic.GenericSlot;
import lombok.extern.slf4j.Slf4j;
import org.qfedu.edu.service.IJwtService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.concurrent.TimeUnit;

/**
 * @author xiaobobo
 * @title: PassSaleSlot
 * @projectName seckill-fy-2401-parent
 * @description: 放行购买的这个slot
 * 这个slot的只要功能是 用户在秒杀的时候要记录秒杀状态
 * @date 2024/11/11  16:40
 */
@Component
@Slf4j
public class PassSaleSlot extends GenericSlot {

    @Autowired
    private IJwtService jwtService;

    @Autowired
    private RedisManager redisManager;

    @Override
    public int getOrder() {
        return Integer.MIN_VALUE+2;
    }

    @Override
    public void dealRequest(HttpServletRequest req, HttpServletResponse resp) {
        //执行到这里 说明用户可以购买这个商品
        //获取商品id
        String classId = req.getParameter("classId");
        //获取用户id
        String token = req.getHeader("token");
        String userId = jwtService.getUserId(token);
        //记录用户购买的状态
        String key=RedisConstance.getBuyStatusPrefix()+userId+":"+classId;
        //接下来向Redis中写入数据
        redisManager.addObject(key, BuyStatus.BUYING.getStatus(),60, TimeUnit.DAYS);
        log.info("PassSaleSlot执行完成....");
    }
}
